package bookstore.service;

import java.util.List;

import bookstore.domain.Book;
import bookstore.domain.CODE;
import bookstore.domain.Order;

public class HostImpl implements Host{
	private List<Book> shelf;   
	private List<Order> orders ;	
	
	public HostImpl(List<Book> shelf,List<Order> orders) { 
		this.shelf = shelf;
		this.orders = orders;
	}	
	
	public List<Book> stockList(){
		return shelf;
	};
	
	public boolean stockAdd(){	
		Book book = new Book();
		book.setInfo();
		return shelf.add(book);
	};
	
	public Book stockModify(Book book){
		if(book != null && book.setInfo());
		return book;
	};
	
	public boolean stockDel(int code){
		boolean isOK = false;
		for(int i=0;i<shelf.size();i++)
			if(shelf.get(i).getCode() == code) {
				if(shelf.remove(i)!=null) isOK=true;				
				break;
			}
		return isOK;
	}
	
	public Book stockView(int code){
		Book book = null;
		for(Book tmp:shelf) 
			if(tmp.getCode()==code) {book = tmp; break;}
		return book;			
	}; 
	
	public List<Order> sellList(){
		return orders;
	};
	
	public int sellTotal(){
		int total = 0;
		for(Order order:orders)
			if(order.getStatus()!=CODE.ORDER_STATUS_CANCEL) total += order.getBook().getPrice();
		return total;
	}
	
	public boolean sellSend(int orderCode){
		boolean isOK = true;
		for(Order order:orders)
			if(order.getCode() == orderCode) 
				order.setStatus(CODE.ORDER_STATUS_SEND);
		return isOK;
	};
	
	public boolean sellCancel(int orderCode){
		boolean isOK = false;
		for(Order order:orders)
			if(order.getCode()==orderCode){
				order.setStatus(CODE.ORDER_STATUS_CANCEL);
				isOK = true;
			}
		return isOK;
	};
	
	public boolean sellAdd(Order order){
		boolean isOK = orders.add(order);
		return isOK;
	}
}
